﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Objects;

namespace InventoryPro.DAL
{
    public class CustomerDAO
    {
        InvEntities context ;
        public CustomerDAO() 
        {
             
        context = new InvEntities(); 
        }

        public List<Customer> GetAllCustomers()
        {

            
            var query = from c in context.Customers                       
                        select c;
 
            //var queryResult = (((ObjectQuery)query).Execute(MergeOption.AppendOnly));
            //             context.Dispose();

           return  query.ToList<Customer>();
            
         }

        public Customer  GetCustomerbyID(string ID)

        {
            Customer customer = null;
            var query = from c in context.Customers
                        where c.CustomerID == ID
                        select c;
            var Q = (((ObjectQuery)query).Execute(MergeOption.AppendOnly));
            foreach (Customer c in Q)
            {
                customer = c;                
            }

            if(customer != null)
            context.Detach(customer);
            return customer;
        }


        public void CreateCustomer(Customer customer)

        {
            context.AddToCustomers(customer);
            context.SaveChanges();

        }


        public void UpdateCustomer(Customer customer)

        {

            context.Attach(customer);
             context.SaveChanges();
        }


        public void DeleteCustomer(string ID)
        {
            Customer customer = null;
            var query = from c in context.Customers
                        where c.CustomerID == ID
                        select c;
            var Q = (((ObjectQuery)query).Execute(MergeOption.AppendOnly));
            foreach (Customer c in Q)
            {
                customer = c;                
            }

            context.DeleteObject(customer);
            
        }




        }
}




